<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.or">
<head>
<meta  charset="utf-8" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>NeptuneGymsSystem</title>
<link rel="stylesheet" href="../layui/css/layui.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin" >
  <div class="layui-header">
    <div class="layui-logo"><h1>健身房</h1></div>
    <!-- 顶部导航 -->
    <ul class="layui-nav layui-bg-cyan layui-layout-left">
      <li class="layui-nav-item">
      	<a href="">会员管理</a>
      	<dl class="layui-nav-child">
      		<dd><a href="../visit/index">会员来访</a></dd>
          	<dd><a href="../member/index">会员管理</a></dd>
            <dd><a href="../card/index_entity">会员卡管理</a></dd>
            <dd><a href="../card/index">会员卡设置</a></dd>
        </dl>
      </li>
      <li class="layui-nav-item">
      	<a href="">商品管理</a>
      	<dl class="layui-nav-child">
            <dd><a href="../goods/index">商品管理</a></dd>
            <dd><a href="../goodsType/index">商品类型</a></dd>
            <dd><a href="../goodsSale/index">消费记录</a></dd>
            <dd><a href="../stock/index">库存管理</a></dd>
            <dd><a href="../operate/index">库存操作记录</a></dd>
          </dl>
      </li>
      <li class="layui-nav-item">
     	 <a href="">课程管理</a>
     	 <dl class="layui-nav-child">
            <dd><a href="/course/index">课程管理</a></dd>
            <dd><a href="/attendCourse/index">操课管理</a></dd>  
            <dd><a href="/searchOrder/index">约课管理</a></dd>          
          </dl>
      </li>
      <li class="layui-nav-item">
      	<a href="">统计报表</a>
      	<dl class="layui-nav-child">
            <dd><a href="http://localhost:8080/change_1">会员消费统计</a></dd>
            <dd><a href="http://localhost:8080/change_2">营业报表统计</a></dd>
            <dd><a href="http://localhost:8080/change_3">员工业绩管理</a></dd>
            <dd><a href="http://localhost:8080/change_7">会员来访统计</a></dd>          
        </dl>
      </li>
      <li class="layui-nav-item">
        <a href="javascript:;">更多操作</a>
        <dl class="layui-nav-child">
          <dd><a href="http://localhost:8080/setting/stuff">员工管理</a></dd>
          <dd><a href="http://localhost:8080/trainer_management">教练管理</a></dd>
          <dd><a href="http://localhost:8080/setting/database">数据库管理</a></dd>         
        </dl>
      </li>
    </ul>
    <ul class="layui-nav layui-layout-right">
      <li class="layui-nav-item">
        <a href="javascript:;">
          <img id="user_img" src="http://t.cn/RCzsdCq" class="layui-nav-img">
          <label id="user_name"></label>
        </a>
        <dl class="layui-nav-child">
          <dd><a id="userInformation" href="#">基本资料</a></dd>
        </dl>
      </li>
      <li class="layui-nav-item"><a href="http://localhost:8080/login_out">退出</a></li>
    </ul>
  </div>
  
  <div class="layui-side layui-bg-black">
    <div class="layui-side-scroll">
      <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
      <ul class="layui-nav layui-bg-cyan layui-nav-tree">
        <li class="layui-nav-item">
          <a class="" href="javascript:;">会员管理</a>
          <dl class="layui-nav-child">
          	<dd><a href="../visit/index">会员来访</a></dd>
          	<dd><a href="../member/index">会员管理</a></dd>
            <dd><a href="../card/index_entity">会员卡管理</a></dd>
            <dd><a href="../card/index">会员卡设置</a></dd>
          </dl>
        </li>
        <li class="layui-nav-item layui-nav-itemed">
          <a href="javascript:;">商品管理</a>
          <dl class="layui-nav-child">
            <dd class="layui-this"><a href="../goods/index">商品管理</a></dd>
            <dd><a href="../goodsType/index">商品类型</a></dd>
            <dd><a href="../goodsSale/index">消费记录</a></dd>
            <dd><a href="../stock/index">库存管理</a></dd>
            <dd><a href="../operate/index">库存操作记录</a></dd>
          </dl>
        </li>
        <li class="layui-nav-item">
          <a href="javascript:;">课程管理</a>
          <dl class="layui-nav-child">
            <dd><a href="/course/index">课程管理</a></dd>
            <dd><a href="/attendCourse/index">操课管理</a></dd>
            <dd><a href="/searchOrder/index">约课管理</a></dd>            
          </dl>
        </li>
         <li class="layui-nav-item">
          <a href="javascript:;">统计报表</a>
          <dl class="layui-nav-child">
            <dd><a href="http://localhost:8080/change_1">会员消费统计</a></dd>
            <dd><a href="http://localhost:8080/change_2">营业报表统计</a></dd>
            <dd><a href="http://localhost:8080/change_3">员工业绩管理</a></dd>
            <dd><a href="http://localhost:8080/change_7">会员来访统计</a></dd>   
          </dl>
        </li>
         <li class="layui-nav-item">
        <a href="javascript:;">更多操作</a>
        <dl class="layui-nav-child">
          <dd><a href="http://localhost:8080/setting/stuff">员工管理</a></dd>
          <dd><a href="http://localhost:8080/trainer_management">教练管理</a></dd>
          <dd><a href="http://localhost:8080/setting/database">数据库管理</a></dd>         
        </dl>
      </li>       
      </ul>
    </div>
  </div>
  
<div class="layui-body">
    <!-- 内容主体区域 -->
				
		<!-- 表格渲染 -->
		<table class="layui-hide" id="goods_dataBox" lay-filter="goods_dataBox" ></table>

		<!-- 分页 -->	
		<div id="goods_pageBox"></div>
		
		<!--  -->
 
<script type="text/html" id="goods_toolBox">
	<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
  	<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	{{# if(d.goodsState=="未上架"){}}
	<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="upper">上架</a>
	{{# }else if(d.goodsState=="已上架"){}}
	<a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="lower">下架</a>
	{{# }}} 
</script>
			
		      	
    
   </div>  
  	<div class="layui-footer" align="center">
  	<div style="padding: 15px;" align="center">
	    <span class="layui-badge-dot"></span>
		<span class="layui-badge-dot layui-bg-orange"></span>
		<span class="layui-badge-dot layui-bg-green"></span>
		<span class="layui-badge-dot layui-bg-cyan"></span>
		<span class="layui-badge-dot layui-bg-blue"></span>
		<span class="layui-badge-dot layui-bg-black"></span>
		<span class="layui-badge-dot layui-bg-gray"></span>
		<span>Copyright ©2019-2020 GYJ.All Rights Reserved.</span>
        <span class="layui-badge-dot layui-bg-gray"></span>
        <span class="layui-badge-dot layui-bg-black"></span>
        <span class="layui-badge-dot layui-bg-blue"></span>
        <span class="layui-badge-dot layui-bg-cyan"></span>
        <span class="layui-badge-dot layui-bg-green"></span>
        <span class="layui-badge-dot layui-bg-orange"></span>
        <span class="layui-badge-dot"></span>
    </div>
    	<hr class="layui-bg-cyan"> 
  	</div>
	</div>		
	
 
	
	
	
	<script src="../layui/layui.js"></script>
	<script src="../js/user.js"></script>
	<script type="text/javascript">
	layui.use(['layer','jquery','form','laypage','table','element'],function(){
		var layer=layui.layer;
		var $=layui.jquery;
		var form=layui.form;
		var laypage=layui.laypage;
		var table=layui.table;						
		var element=layui.element;
		
		$.ajax({					//为头像和用户名赋值
			url:'/user/get_user',
			//dataType:'json',
			type:'get',			
			success:function(data){
				document.getElementById("user_img").src=data.image;
				document.getElementById("user_name").innerHTML=data.name;
			}
		});
		
		//真分页
		$(function(){
			getData(0);
		})
		function getData(pageStart){
			layui.use(['table','laypage'],function(){
				var table=layui.table;
				var laypage=layui.laypage;
				table.render({
					elem:'#goods_dataBox'
					,url:'/goods/getGoodsBtween'
					,method:"get"
					,where:{
						pageStart:pageStart,
						pageSize:10
					}
					,parseData:function(res){				
						console.log(res)
						return{
							"code":0
							,"msg":""
							,"count":res.rowSize
							,"data":res.data
							,"curr":res.pageStart
							,"limit":res.limit
						}
					}
					,toolbar: 'default' //开启头部工具栏，并为其绑定左侧模板
					,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
						title: '提示'
					    ,layEvent: 'LAYTABLE_TIPS'
					    ,icon: 'layui-icon-tips'
					 }]
					//,cellMinWidth:100
					,cols:[
				    		[
						      {type: 'checkbox', fixed: 'left'}
						      ,{field:'goodsId', title:'商品编号', width:140, fixed: 'left', unresize: true, sort: true}
						      ,{field:'goodsName', title:'商品名称', width:140}	      
						      ,{field:'goodsType', title:'类型', width:120 }
						      ,{field:'goodsPrice', title:'单价', width:120, sort: true }
						      ,{field:'goodsUnit', title:'单位' ,width:120}
						      ,{field:'goodsNote', title:'备注', width:130}
						      ,{field:'goodsState', title:'状态', width:120}
						      ,{fixed: 'right', title:'操作', width: 250, align:'center', toolbar: '#goods_toolBox'}
				   			]
				    	]
					,done:function(res,curr,count){
						console.log(res);
						laypage.render({
							elem:'goods_pageBox'
							,count:res.count
							,limit:res.limit
							,layout: ['count','prev','page','next','skip']
							,curr:res.curr/10+1
							,jump:function(obj,first){//触发分页后的回调
								if(!first){//点击跳转触发函数自身，并传递当前页：obj.curr							
									res.pageStart=obj.limit*(obj.curr-1);
									console.log(res.pageStart);
									getData(res.pageStart);														
								}//if
							}//jump function
						});
						$(document).ready(function(){
							var complete="已上架";
							var uncomplete="未上架";		
							$("table tr td").find("div").each(function(){//遍历页面table中的div
								//alert(complete);
								//alert(uncomplete);
								//alert($(this).html());
								if($(this).html()==complete){ //div值为complete(已上架)则修改这一个div的样式
									$(this).css("color","green");
								}
								else if($(this).html()==uncomplete){//div值为uncomplete(未上架)则修改这一个div的样式
									$(this).css("color","red");
								}	
							});
						});
					}
				});
			});
		}//function
		//表格顶部工具栏
		table.on('toolbar(goods_dataBox)',function(res){
			 var checkStatus=table.checkStatus(res.config.id);
			 var data=checkStatus.data;
			 var event=res.event;
			 var i=0;
			 switch(res.event){
			 	case 'add':
			 		add();
			 		break;
			 	case 'update': 
			 		if(data.length==0){
			 			layer.msg('请选择一行');
			 		}
			 		else if(data.length>1){
			 			layer.msg('只能同时编辑一个');
			 		}
			 		else{
			 			//layer.alert('编辑[id]:'+checkStatus.data[0].goodsName);
			 			update(checkStatus.data[0].goodsId)
			 		}
			 		break;
			 	case 'delete':
			 		if(data.length===0){
			 			layer.msg('请选择一行');
			 		}
			 		else if(data.length>1){
			 			layer.confirm('真的删除行么', function(index){
				 			for(i=0;i<data.length;i++){
				 				deleteGoodsById(checkStatus.data[i].goodsId);
				 			}		
				 			layer.close(index);
			 			});			 			
			 		}
			 		else{
			 			layer.confirm('真的删除行么', function(index){
			 				deleteGoodsById(checkStatus.data[0].goodsId);
			 		        layer.close(index);
			 		        //向服务端发送删除指令
			 		      });
			 		}
			 		break; 
			 }
		 });
		//表格内部工具栏
		//监听行工具事件
		table.on('tool(goods_dataBox)', function(obj){ //注：tool 是工具条事件名，goods_toolBox 是 table 原始容器的属性 lay-filter="对应的值"
			var data = obj.data //获得当前行数据
		    ,layEvent = obj.event; //获得 lay-event 对应的值
		    if(layEvent === 'detail'){		    	
		    	detail(data.goodsId);
		    } 
		    else if(layEvent === 'del'){
		    	layer.confirm('真的删除行么', function(index){
		        	deleteGoodsById(data.goodsId); 
		        	layer.close(index);
		        	//向服务端发送删除指令
		      	 });
		    } 
		    else if(layEvent === 'edit'){
		    	update(data.goodsId);
		    }
		    else if(layEvent==="upper"){
		    	setStateToYes(data.goodsId);
		    }
		    else if(layEvent==="lower"){
		    	setStateToNo(data.goodsId);
		    }
		  });
		//复选框时间监听
		table.on('checkbox(goods_dataBox)', function(obj){
			var length = $('input:checked').length;
			console.log(length);
			console.log(obj.checked); //当前是否选中状态
			console.log(obj.data); //选中行的相关数据
			console.log(obj.type); //如果触发的是全选，则为：all，如果触发的是单选，则为：one
		});
		
		function detail(id){
			layer.open({
				type:2
				,title:"商品详情"
				,content:'../goods/detail?id='+id
				,area:['300px','600px']												
			});
		}
		
		function add(){
			layer.open({
				type:2,
				title:"新增商品",
				content:'../goods/toAdd',
				area:['450px','95%'],	
				end:function(){										
					getData(0); 					
				}			
			});
			$("#goodsType").empty();
		}
		
		function update(id){
			layer.open({
				type:2
				,title:"修改商品"
				,content:'../goods/selectById?id='+id
				,area:['400px','550px']
				,end:function(){
					getData(0);
				}
			});
			
		} 
		
		function deleteGoodsById(id){
			$.ajax({
				type:'post'
				,url:'../goods/deleteGoodsById?id='+id
				,success:function(data){
					if(data==="true")
						layer.msg("删除成功");
					else
						layer.msg("删除失败");
					getData(0);
				}
			});
		}		
		
		function setStateToYes(id){
			$.ajax({
				type:'post'
				,url:'../goods/setStateToYes?goodsId='+id
				,success:function(){
					//alert(id);
					getData(0);
				}
				,error:function(){
					alert("异常");
				}				
			});
		}
		
		function setStateToNo(id){
			$.ajax({
				type:'post'
				,url:'../goods/setStateToNo?goodsId='+id
				,success:function(){
					//alert(id);
					getData(0);
				}
				,error:function(){
					alert("异常");
				}				
			});
		}
	});							
	</script>
</body>
</html>